/**
 * 2018年12月6日下午1:55:19
 * @author 谢进财
 */
package com.baihang.platform.service;

import java.util.Random;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baihang.framework.kit.DateKit;
import com.baihang.framework.kit.ShiroKit;
import com.baihang.framework.kit.database.RedisKit;
import com.baihang.framework.service.BaseService;
import com.baihang.platform.dao.IndexDao;
import com.baihang.project.ds.config.SystemConfig;

import redis.clients.jedis.Jedis;

/**
 * 主页服务
 *
 */
@Service
public class IndexService extends BaseService {
	private static Logger logger = LoggerFactory.getLogger(IndexService.class);
	@Autowired
	private IndexDao dao;
	private Jedis jedis = RedisKit.getJedis();
	public boolean login(String D_Tel, String D_Code,String D_Card) {
		logger.info(String.format("%s:%s", D_Tel,D_Code));
		try {
			if("1".equals(jedis.hmget("systemConfig","YzType").get(0))) {
				System.err.println("验证码");
				SecurityUtils.getSubject().login(new UsernamePasswordToken(D_Tel, "", false, null));
			}else {
				System.err.println("身份证");
				SecurityUtils.getSubject().login(new UsernamePasswordToken(D_Tel, D_Card, false, null));
			}
		} catch (AuthenticationException e) {
			System.err.println("验证未通过");
			return false;
		} 
		return true;
	}
	
	public boolean checkTel(String D_Tel) {
		try {
		   dao.queryForString(String.format("select D_Tel from t_user where D_Tel='%s' ", D_Tel));
		}catch(Exception ex) {
			return false;
		}
		
		System.err.println("进行查号码");
		return true;
	}
	
	public boolean sendCode(String D_Tel) {
		if(checkTel(D_Tel)) {
			//生成6位手机验证码
			String verifyCode = String.valueOf(new Random().nextInt(899999) + 100000);	
			SystemConfig.codeMap.put(D_Tel, verifyCode);//存储短信，用于身份验证
		}else {
			return false;
		}
		return true;
	}
	
	//更新用户登录时间
	public boolean updateLoginTime() {
		dao.update(String.format("update t_user set D_lastLoginTime='%s' where D_Tel=%s", DateKit.getCurrentDate(DateKit.Format_DateTime),ShiroKit.getCurrentUser().getUserName()));
		return true;
	}
	 public static void main(String[] args) {
		System.err.println(String.valueOf(new Random().nextInt(899999) + 100000));
	}
}
